package GUI.main.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import officeserver.log_error.AppointmentException;
import officeserver.log_error.UserException;
import officeserver.users.PersonalInfo;

import GUI.calendar.CalendarGridCellPanel;
import GUI.calendar.CalendarGridPanel;
import GUI.main.MainClient;

/**
 * @author mramsey3
 * 
 */
public class AddAppointmentDialog extends JDialog implements ActionListener {

    private static final long serialVersionUID = 1L;

    private JPanel titlePanel;

    private JPanel centerPanel;

    private JPanel firstNamePanel;
    private JTextField firstNameTextField;
    private JButton firstNameEditButton;
    private JPanel lastNamePanel;
    private JTextField lastNameTextField;
    private JButton lastNameEditButton;
    private JPanel reasonPanel;
    private JTextField reasonTextField;
    private JButton reasonEditButton;
    private JPanel doctorPanel;
    private JComboBox doctorComboBox;
    private JPanel timePanel;
    private JComboBox timeComboBox;

    private JPanel buttonPanel;
    private JButton requestButton;
    private JButton cancelButton;

    private int globalDayCount;
    private CalendarGridCellPanel cell;
    private CalendarGridPanel grid;

    /**
     * @param globalDayCount
     * @param cell
     * @param grid
     */
    public AddAppointmentDialog(int globalDayCount, CalendarGridCellPanel cell,
	    CalendarGridPanel grid) {
	this.globalDayCount = globalDayCount;
	this.cell = cell;
	this.grid = grid;
	setSize(new Dimension(400, 300));
	setLocation(50, 50);
	setLayout(new BorderLayout());

	add(getTitlePanel(), BorderLayout.NORTH);

	add(getCenterPanel(), BorderLayout.CENTER);
    }

    /**
     * @return
     */
    private JPanel getTitlePanel() {
	if (titlePanel == null) {
	    titlePanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(new Color(100, 150, 220));
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
		    g.drawString("add appointment:", 24, getHeight() - 8);
		}
	    };
	    titlePanel.setPreferredSize(new Dimension(400, 60));
	}
	return titlePanel;
    }

    /**
     * @return
     */
    private JPanel getCenterPanel() {
	if (centerPanel == null) {
	    centerPanel = new JPanel();

	    centerPanel.setLayout(new BoxLayout(centerPanel,
		    BoxLayout.PAGE_AXIS));

	    centerPanel.add(getFirstNamePanel());
	    centerPanel.add(getLastNamePanel());
	    centerPanel.add(getReasonPanel());
	    centerPanel.add(getDoctorPanel());
	    centerPanel.add(getTimePanel());
	    centerPanel.add(getButtonPanel());
	}
	return centerPanel;
    }

    /**
     * @return
     */
    private JPanel getFirstNamePanel() {
	if (firstNamePanel == null) {
	    firstNamePanel = new JPanel();
	    firstNamePanel.setLayout(new BorderLayout());

	    JLabel label = new JLabel("  First Name:");
	    label.setPreferredSize(new Dimension(80, 25));
	    firstNamePanel.add(label, BorderLayout.WEST);

	    JPanel centerPanel = new JPanel();
	    centerPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.anchor = GridBagConstraints.WEST;
	    gbc1.weightx = 1;
	    centerPanel.add(getFirstNameTextField(), gbc1);
	    firstNamePanel.add(centerPanel, BorderLayout.CENTER);

	    JPanel rightPanel = new JPanel();
	    rightPanel.setPreferredSize(new Dimension(60, 25));
	    rightPanel.setLayout(new GridBagLayout());
	    rightPanel.add(getFirstNameEditButton());
	    firstNamePanel.add(rightPanel, BorderLayout.EAST);
	}
	return firstNamePanel;
    }

    /**
     * @return
     */
    private JTextField getFirstNameTextField() {
	if (firstNameTextField == null) {
	    firstNameTextField = new JTextField();
	    try {
		firstNameTextField.setText(MainClient.getConnection()
			.getSelfPersonalInfo().getFirstName());
	    } catch (UserException e) {
		e.printStackTrace();
	    }
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    firstNameTextField.setPreferredSize(new Dimension(170, 20));
	    firstNameTextField.setEditable(false);
	}
	return firstNameTextField;
    }

    /**
     * @return
     */
    private JButton getFirstNameEditButton() {
	if (firstNameEditButton == null) {
	    firstNameEditButton = new JButton("edit");
	    firstNameEditButton.addActionListener(this);
	    firstNameEditButton.setMargin(new Insets(0, 0, 0, 0));
	    firstNameEditButton.setPreferredSize(new Dimension(50, 20));
	}
	return firstNameEditButton;
    }

    /**
     * @return
     */
    private JPanel getLastNamePanel() {
	if (lastNamePanel == null) {
	    lastNamePanel = new JPanel();
	    lastNamePanel.setLayout(new BorderLayout());

	    JLabel label = new JLabel("  Last Name:");
	    label.setPreferredSize(new Dimension(80, 25));
	    lastNamePanel.add(label, BorderLayout.WEST);

	    JPanel centerPanel = new JPanel();
	    centerPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.anchor = GridBagConstraints.WEST;
	    gbc1.weightx = 1;
	    centerPanel.add(getLastNameTextField(), gbc1);
	    lastNamePanel.add(centerPanel, BorderLayout.CENTER);

	    JPanel rightPanel = new JPanel();
	    rightPanel.setPreferredSize(new Dimension(60, 25));
	    rightPanel.setLayout(new GridBagLayout());
	    rightPanel.add(getLastNameEditButton());
	    lastNamePanel.add(rightPanel, BorderLayout.EAST);
	}
	return lastNamePanel;
    }

    /**
     * @return
     */
    private JTextField getLastNameTextField() {
	if (lastNameTextField == null) {
	    lastNameTextField = new JTextField();
	    try {
		lastNameTextField.setText(MainClient.getConnection()
			.getSelfPersonalInfo().getLastName());
	    } catch (UserException e) {
		e.printStackTrace();
	    }
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    lastNameTextField.setPreferredSize(new Dimension(170, 20));
	    lastNameTextField.setEditable(false);
	}
	return lastNameTextField;
    }

    /**
     * @return
     */
    private JButton getLastNameEditButton() {
	if (lastNameEditButton == null) {
	    lastNameEditButton = new JButton("edit");
	    lastNameEditButton.addActionListener(this);
	    lastNameEditButton.setMargin(new Insets(0, 0, 0, 0));
	    lastNameEditButton.setPreferredSize(new Dimension(50, 20));
	}
	return lastNameEditButton;
    }

    /**
     * @return
     */
    private JPanel getReasonPanel() {
	if (reasonPanel == null) {
	    reasonPanel = new JPanel();
	    reasonPanel.setLayout(new BorderLayout());

	    JLabel label = new JLabel("  Reason:");
	    label.setPreferredSize(new Dimension(80, 25));
	    reasonPanel.add(label, BorderLayout.WEST);

	    JPanel centerPanel = new JPanel();
	    centerPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.anchor = GridBagConstraints.WEST;
	    gbc1.weightx = 1;
	    centerPanel.add(getReasonTextField(), gbc1);
	    reasonPanel.add(centerPanel, BorderLayout.CENTER);

	    JPanel rightPanel = new JPanel();
	    rightPanel.setPreferredSize(new Dimension(60, 25));
	    rightPanel.setLayout(new GridBagLayout());
	    rightPanel.add(getReasonEditButton());
	    reasonPanel.add(rightPanel, BorderLayout.EAST);
	}
	return reasonPanel;
    }

    /**
     * @return
     */
    private JTextField getReasonTextField() {
	if (reasonTextField == null) {
	    reasonTextField = new JTextField();
	    reasonTextField.setText("Too many all nighters coding cs");
	    reasonTextField.setPreferredSize(new Dimension(170, 20));
	    reasonTextField.setEditable(false);
	}
	return reasonTextField;
    }

    /**
     * @return
     */
    private JButton getReasonEditButton() {
	if (reasonEditButton == null) {
	    reasonEditButton = new JButton("edit");
	    reasonEditButton.addActionListener(this);
	    reasonEditButton.setMargin(new Insets(0, 0, 0, 0));
	    reasonEditButton.setPreferredSize(new Dimension(50, 20));
	}
	return reasonEditButton;
    }

    /**
     * @return
     */
    private JPanel getDoctorPanel() {
	if (doctorPanel == null) {
	    doctorPanel = new JPanel();
	    doctorPanel.setLayout(new BorderLayout());

	    JLabel label = new JLabel("  Reason:");
	    label.setPreferredSize(new Dimension(80, 25));
	    doctorPanel.add(label, BorderLayout.WEST);

	    JPanel centerPanel = new JPanel();
	    centerPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.anchor = GridBagConstraints.WEST;
	    gbc1.weightx = 1;
	    centerPanel.add(getDoctorComboBox(), gbc1);
	    doctorPanel.add(centerPanel, BorderLayout.CENTER);
	}
	return doctorPanel;
    }

    /**
     * @return
     */
    private JComboBox getDoctorComboBox() {
	if (doctorComboBox == null) {
	    ArrayList<PersonalInfo> doctors = new ArrayList<PersonalInfo>();
	    try {
		doctors = MainClient.getConnection().getDoctors();
	    } catch (UserException e) {
		e.printStackTrace();
	    }
		catch (AppointmentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ClassNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    String[] docs = new String[doctors.size()];
	    for (int i = 0; i < doctors.size(); i++) {
		docs[i] = doctors.get(i).getFirstName() + " "
			+ doctors.get(i).getLastName();
	    }
	    doctorComboBox = new JComboBox(docs);
	    doctorComboBox.setPreferredSize(new Dimension(170, 20));
	    doctorComboBox.setSelectedIndex(0);
	}
	return doctorComboBox;
    }

    /**
     * @return
     */
    private JPanel getTimePanel() {
	if (timePanel == null) {
	    timePanel = new JPanel();
	    timePanel.setLayout(new BorderLayout());

	    JLabel label = new JLabel("  Time:");
	    label.setPreferredSize(new Dimension(80, 25));
	    timePanel.add(label, BorderLayout.WEST);

	    JPanel centerPanel = new JPanel();
	    centerPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.anchor = GridBagConstraints.WEST;
	    gbc1.weightx = 1;
	    centerPanel.add(getTimeComboBox(), gbc1);
	    timePanel.add(centerPanel, BorderLayout.CENTER);
	}
	return timePanel;
    }

    /**
     * @return
     */
    private JComboBox getTimeComboBox() {
	if (timeComboBox == null) {
	    boolean[][] schedule = null;
	    try {
		schedule = MainClient.getConnection().selfCheckCalendar(
			globalDayCount);
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	    int[] slots = new int[schedule.length];
	    for (int i = 0; i < schedule.length; i++) {
		if (schedule[i][getDoctorComboBox().getSelectedIndex()]) {
		    slots[i] = i;
		} else {
		    slots[i] = -1;
		}
	    }
	    String[] times = new String[slots.length];
	    for (int j = 0; j < slots.length; j++) {
		if (slots[j] >= 0) {
		    if (((slots[j] < 3))) {
			times[j] = Integer.toString(((slots[j] + 9)))
				+ ":00 AM";
		    } else if (slots[j] == 3) {
			times[j] = Integer.toString((slots[j] + 9)) + ":00 PM";
		    } else {
			times[j] = Integer.toString((slots[j] + 9) % 12)
				+ ":00 PM";
		    }
		} else {
		    times[j] = "---";
		}

	    }
	    timeComboBox = new JComboBox(times);
	    timeComboBox.setPreferredSize(new Dimension(170, 20));
	}
	return timeComboBox;
    }

    /**
     * @return
     */
    private JPanel getButtonPanel() {
	if (buttonPanel == null) {
	    buttonPanel = new JPanel();
	    buttonPanel.setLayout(new GridBagLayout());

	    buttonPanel.add(getRequestButton());
	    buttonPanel.add(Box.createHorizontalStrut(10));
	    buttonPanel.add(getCancelButton());
	}
	return buttonPanel;
    }

    /**
     * @return
     */
    private JButton getRequestButton() {
	if (requestButton == null) {
	    requestButton = new JButton("Request");
	    requestButton.addActionListener(this);
	    requestButton.setPreferredSize(new Dimension(80, 30));
	}
	return requestButton;
    }

    /**
     * @return
     */
    private JButton getCancelButton() {
	if (cancelButton == null) {
	    cancelButton = new JButton("Cancel");
	    cancelButton.addActionListener(this);
	    cancelButton.setPreferredSize(new Dimension(80, 30));
	}
	return cancelButton;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == getFirstNameEditButton()) {
	    String newFirstName = JOptionPane.showInputDialog("First name:");
	    getFirstNameTextField().setText(newFirstName);
	} else if (e.getSource() == getLastNameEditButton()) {
	    String newLastName = JOptionPane.showInputDialog("Last name:");
	    getLastNameTextField().setText(newLastName);
	} else if (e.getSource() == getReasonEditButton()) {
	    String newReason = JOptionPane.showInputDialog("Reason:");
	    getReasonTextField().setText(newReason);
	} else if (e.getSource() == getRequestButton()) {
	    try {
		MainClient.getConnection().addSelfAppointment(
			getDoctorComboBox().getSelectedIndex(), globalDayCount,
			timeComboBox.getSelectedIndex());
	    } catch (Exception e1) {
		e1.printStackTrace();
	    }
	    setVisible(false);
	    grid.updateCell(cell);
	    cell.repaint();
	} else if (e.getSource() == getCancelButton()) {
	    setVisible(false);
	}
    }
}
